package com.gzy.mylog.controller;

import com.gzy.mylog.core.R;
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.xmlpull.v1.XmlPullParserException;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

@CrossOrigin
@RestController
public class FileController {
    private String endpoint = "http://47.113.110.192:9000";
    private String accessKey = "xxxx";
    private String secretKey = "xxxxxxxx";
    private String bucketName = "bucket1";

    MinioClient minioClient;

    @PostConstruct
    void init(){
        try {
            minioClient = new MinioClient(endpoint, accessKey, secretKey);
        } catch (InvalidEndpointException | InvalidPortException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/upload")
    R upload(@RequestParam("file") MultipartFile file) {

        // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
        // asdasdasdasdas-bg.png
        // 文件的 md5
        // UUID
        // 时间戳 - 有可能会重复
        try {

            boolean isExist = minioClient.bucketExists("bucket1");
            if (!isExist) {
                // 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
                minioClient.makeBucket(bucketName);
            }

            String objName = UUID.randomUUID().toString() + "--" + file.getOriginalFilename();
            minioClient.putObject(bucketName,objName,file.getInputStream(),file.getContentType());
            return new R() {{
                setData(objName);
            }};
        } catch (InvalidKeyException | NoSuchAlgorithmException | NoResponseException | XmlPullParserException | InvalidBucketNameException | InsufficientDataException | ErrorResponseException | InternalException | IOException e) {
            e.printStackTrace();
            return new R() {{
                setCode(Code.ERR);
                setMessage("文件桶不存在，保存失败.");
            }};
        } catch (RegionConflictException e) {
            e.printStackTrace();
            return new R() {{
                setCode(Code.ERR);
                setMessage("创建文件桶失败，保存失败.");
            }};
        } catch (InvalidArgumentException e) {
            e.printStackTrace();
            return new R() {{
                setCode(Code.ERR);
                setMessage("转存失败，保存失败.");
            }};
        }



    }

    @GetMapping("/download/{objName}")
    void download(@PathVariable String objName, HttpServletResponse response){
        try {
            String url = minioClient.presignedGetObject(bucketName, objName);
            response.sendRedirect(url);
        } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException | InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException | InternalException | InvalidExpiresRangeException e) {
            e.printStackTrace();

        }
    }
}
